import unittest

import requests
from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from util import parser_html, read_json
from parameterized import parameterized


class TestTender(unittest.TestCase):
    # 初始化
    def setUp(self) -> None:
        # 获取session对象
        self.session = requests.session()
        # 获取ApiTender对象
        self.tender = ApiTender(self.session)
        # 登录成功
        ApiRegisterLogin(self.session).api_login()

    # 结束
    def tearDown(self) -> None:
        # 关闭session
        self.session.close()

    # 投资测试方法
    @parameterized.expand(read_json("tender.json", "tender"))
    def test01_tender(self, tender_id, amount, expect_text):
        try:
            # 调用投资接口方法
            result = self.tender.api_tender(tender_id, amount)
            if amount == "":
                print(result.json())
                # 断言
                self.assertIn(expect_text, result.text)
            else:
                # 提取参数
                r = parser_html(result)
                # 三方投资
                result = self.session.post(url=r[0], data=r[1])
                print(result.text)
                # 断言
                self.assertIn(expect_text, result.text)
        except Exception as e:
            # 日志
            print("错误原因：", e)
            # 抛异常
            raise
